using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

namespace Impact.Model.Objects
{
    /// <summary>
    /// PageImage is a class that represents an image for display on a view, usually rendered as an
    /// &lt;IMG&gt; tag.  This class provides additional functionality to a lone string filename, 
    /// such as support of randomly selected an image within a folder.
    /// </summary>
    public class PageImage
    {
        private string storageString;

        /// <summary>
        /// default constructor.
        /// </summary>
        /// <param name="storageString">the string that this object is store as in the database</param>
        public PageImage(string storageString)
        {
            this.storageString = storageString;
        }

        /// <summary>
        /// Gets the next image to display.  if random returns the next image below the threshold, 
        /// if constant returns the same image.
        /// </summary>
        public string NextImage
        {
            get
            {
                string output = null;
                string subString = storageString.Substring(4, storageString.Length - 4);
                if (storageString.StartsWith("IMG:"))
                {
                    // TODO: need to pull this from config
                    string userFilesPath = "UserFiles";
                    output = userFilesPath + subString;
                }
                else if (storageString.StartsWith("RND:"))
                {
                    output = GetRandomFilename(subString);
                }
                return "~/"+ output.Replace("\\", "/");
            }
        }

        private string GetRandomFilename(string subString)
        {
            List<string> filenames = GetFilenames(subString);
            Random random = new Random();
            return filenames[random.Next(filenames.Count)];
        }

        private List<string> GetFilenames(string subString)
        {
            // TODO: is there a better way of doing this?
            // can we store the paths from admin differently?
            Assembly currentAsm = Assembly.GetAssembly(this.GetType());
            Uri codebase = new Uri(currentAsm.CodeBase);
            DirectoryInfo execDir = new DirectoryInfo(codebase.AbsolutePath);
            DirectoryInfo appRoot = execDir.Parent.Parent;
            // TODO: need to pull this from config
            string userFilesPath = "UserFiles";

            string filePath = string.Format("{0}\\{1}{2}", appRoot.FullName, userFilesPath, subString);

            List<string> filenames = new List<string>();
            foreach (string filename in Directory.GetFiles(filePath))
            {
                filenames.Add(filename.Replace(appRoot.FullName + "\\", string.Empty));
            }
            return filenames;
        }

        /// <summary>
        /// Gets the storage string that was used to create the image
        /// </summary>
        public string Path
        {
            get { return storageString; }
        }

    }
}
